'use strict';
const Minio = require('minio')
const Controller = require('egg').Controller;
const formidable = require('formidable')

class UploadController extends Controller {
  async upload() {
    let form = new formidable.IncomingForm();
    const {
      ctx,
      service,
      app
    } = this
    // const payload = ctx.request.body || {}
    form.parse(ctx.request.body, function (err, fields, files) {
      if (err) {
        throw err;
      }
      console.log(fields);
      console.log(files);
      res.end('success');
    })
    // console.log('payload', payload)
    let minioClient = new Minio.Client({
      endPoint: '49.232.149.113', // http://49.232.149.113:38205
      port: 9000,
      useSSL: false, // 不需要https
      accessKey: 'minioadmin', // 账号
      secretKey: 'minioadmin' // 密码
    });
    try {
      const res = await minioClient.fPutObject('tuchuang', file.file.name, JSON.stringify(imgFile), {
        'Content-Type': 'application/octet-stream'
      }, function (err, etag) {
        console.log('err', err)
        return etag // err should be null
      })
      console.log('res---', res)
    } catch (err) {
      console.log('catch', err)
    }
  }
}

module.exports = UploadController;